﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetBay.Core.DataAccess;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.ComponentModel;
using NetBay.Core.Extensions;
using System.Data;
using NetBay.Core.Log;
using System.Data.OracleClient;
using System.Configuration;
#endregion

namespace Rio.Windows.Services.DataAccessLayer
{
    public class DataAccessManager
    {
        #region Enum
        /// <summary>
        /// List of DB functions to access data
        /// </summary>
        public enum DbFunction
        {
            [DescriptionAttribute("")]
            None,
            [DescriptionAttribute("P_ALERTES_Get")]
            GetAllAlertsOfToday,
            [DescriptionAttribute("P_GROUP_Get_ByAlert")]
            GetAllGroupsOfAnAlert,
            [DescriptionAttribute("P_USERS_Get_ByGroup")]
            GetAllUsersFromGroup,
            [DescriptionAttribute("P_ALERTES_Update")]
            UpdateAlert,
            [DescriptionAttribute("P_ALERTES_ExecuteAction")]
            ExecuteAction,
        }
        #endregion

        #region Init
        /// <summary>
        /// Initialize this class
        /// </summary>
        /// <param name="p_sDirectory">Configuration file's directory for DataAccess</param>
        /// <param name="p_sFileName">Configuration's name for DataAccess</param>
        /// <param name="p_sDataBaseConnection">Alias of Connexionstring for DataAccess</param>
        public static void Init(string p_sDirectory, string p_sFileName, string p_sDataBaseConnection)
        {
            DbHelper.Init(p_sDirectory, p_sFileName, p_sDataBaseConnection);
        }
        #endregion

        /// <summary>
        /// Is the database online
        /// </summary>
        /// <returns>True if the database is online</returns>
        public static bool IsDataBaseActive()
        {
            bool l_bReturn = true;
            try
            {
                DbConnection l_oDbConnection = DbHelper.DataBase.CreateConnection();
                l_oDbConnection.Open();
            }
            catch
            {
                l_bReturn = false;
            }
            return l_bReturn;
        }

        /// <summary>
        /// Get All alerts of today
        /// </summary>
        /// <returns>A datareader containing all alerts</returns>
        public static IDataReader GetAlertOfTheDay()
        {
            LogManager.Instance().LogDebug("GetAlertOfTheDay()....");
            
            Database db = DbHelper.DataBase;

            DbCommand dbCommand = db.GetStoredProcCommand(DbFunction.GetAllAlertsOfToday.szGetDescription());
            
            var l_bOutPutParameter = false;
            switch ( ConfigurationManager.AppSettings.Get("Rio.Windows.Services.DataBaseConnection") )
            {
                case "RIO.BDD.Oracle":
                    l_bOutPutParameter = true;
                    break;
            }

            if ( l_bOutPutParameter && true )
            {
                var l_oOracleOutputParameter = new OracleParameter("O_Results", OracleType.Cursor);
                l_oOracleOutputParameter.Direction = ParameterDirection.Output;
                dbCommand.Parameters.Add(l_oOracleOutputParameter);
            }


            return db.ExecuteReader(dbCommand);

        }
        
        /// <summary>
        /// Get All Group of an alert
        /// </summary>
        /// <param name="p_lAlertId">The identifier of the alert</param>
        /// <returns>A datareader containing all alerts</returns>
        public static IDataReader GetGroupsOfAnAlert(long p_lAlertId)
        {
            LogManager.Instance().LogDebug("GetGroupsOfAnAlert(p_lAlertId<" + p_lAlertId + ">)....");
            
            Database db = DbHelper.DataBase;

            DbCommand dbCommand = db.GetStoredProcCommand(DbFunction.GetAllGroupsOfAnAlert.szGetDescription());
            db.AddInParameter(dbCommand, "p_lAlertId", DbType.Int32, p_lAlertId);


            var l_bOutPutParameter = false;
            switch ( ConfigurationManager.AppSettings.Get("Rio.Windows.Services.DataBaseConnection") )
            {
                case "RIO.BDD.Oracle":
                    l_bOutPutParameter = true;
                    break;
            }

            if ( l_bOutPutParameter && true )
            {
                var l_oOracleOutputParameter = new OracleParameter("O_Results", OracleType.Cursor);
                l_oOracleOutputParameter.Direction = ParameterDirection.Output;
                dbCommand.Parameters.Add(l_oOracleOutputParameter);
            }

            return db.ExecuteReader(dbCommand);

        }

        /// <summary>
        /// Get All Users of a group
        /// </summary>
        /// <param name="p_lGroupId">The identifier of the group</param>
        /// <returns>A datareader containing all users of a group</returns>
        public static IDataReader GetUsersFromAGroup(long p_lGroupId)
        {
            LogManager.Instance().LogDebug("GetUsersFromAGroup(p_lGroupId<" + p_lGroupId + ">)....");

            Database db = DbHelper.DataBase;

            DbCommand dbCommand = db.GetStoredProcCommand(DbFunction.GetAllUsersFromGroup.szGetDescription());
            db.AddInParameter(dbCommand, "p_lGroupId", DbType.Int32, p_lGroupId);


            var l_bOutPutParameter = false;
            switch ( ConfigurationManager.AppSettings.Get("Rio.Windows.Services.DataBaseConnection") )
            {
                case "RIO.BDD.Oracle":
                    l_bOutPutParameter = true;
                    break;
            }

            if ( l_bOutPutParameter && true )
            {
                var l_oOracleOutputParameter = new OracleParameter("O_Results", OracleType.Cursor);
                l_oOracleOutputParameter.Direction = ParameterDirection.Output;
                dbCommand.Parameters.Add(l_oOracleOutputParameter);
            }


            return db.ExecuteReader(dbCommand);

        }

        /// <summary>
        /// Executes the alert action.
        /// </summary>
        /// <param name="alertId">The alert id.</param>
        public static void ExecuteAlertAction(long alertId)
        {
            LogManager.Instance().LogDebug("ExecuteAlertAction(p_lGroupId<" + alertId + ">)....");

            Database db = DbHelper.DataBase;
            DbCommand dbCommand = db.GetStoredProcCommand(DbFunction.ExecuteAction.szGetDescription());
            db.AddInParameter(dbCommand, "p_lAlertId", DbType.Int32, alertId);

            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// Update an alert
        /// </summary>
        /// <param name="p_lAlertId">The identifier of the alert</param>
        /// <param name="p_lUserId">The identifier of the user</param>
        /// <param name="p_lStatut">The identifier of the new status</param>
        public static void UpdateAlert(long p_lAlertId, long p_lUserId, long p_lStatut)
        {
            LogManager.Instance().LogDebug("UpdateAlert(p_lAlertId<" + p_lAlertId + ">p_lUserId<" + p_lUserId + ">p_lStatut<" + p_lStatut + ">)....");

            Database db = DbHelper.DataBase;

            DbCommand dbCommand = db.GetStoredProcCommand(DbFunction.UpdateAlert.szGetDescription());
            db.AddInParameter(dbCommand, "p_lAlertId", DbType.Int32, p_lAlertId);
            db.AddInParameter(dbCommand, "p_lUserId", DbType.Int32, p_lUserId);
            db.AddInParameter(dbCommand, "p_lStatut", DbType.Int32, p_lStatut);

            db.ExecuteScalar(dbCommand);
        }
    }
}
